#include "Output.h"
#include "Container.h"

using namespace std;

Output::Output(Container* parent, string name, int x, int y, int w, int h, int text_h) {
    this->parent = parent;
    
    this->name = name;
    this->x = x;
    this->y = y;
    this->w = w;
    this->h = h;
    
    l_text.push_back("Initialisation");
    
    police = TTF_OpenFont("times.ttf", text_h);
    couleur = SDL_Color();
    couleur.r = 255;
    couleur.g = 255;
    couleur.b = 255;
    
    SDL_render = SDL_CreateRGBSurface(SDL_HWSURFACE, w, h, 32, 0, 0, 0, 0);
}

Output::~Output() {
    SDL_FreeSurface(SDL_render);
    SDL_FreeSurface(texteSurface);
}

SDL_Surface* Output::updateRender() {
    unsigned int i;
    SDL_Rect position;
    
    // Affiche le container
    SDL_FillRect(SDL_render, NULL, SDL_MapRGB(SDL_render->format, 0, 0, 0));
    
    // Affiche les label
    for(i = 0; i < l_text.size(); i++) {
        texteSurface = TTF_RenderText_Blended(police, l_text[i].c_str(), couleur);
        
        position.x = 0;
        position.y = i * texteSurface->h;
        SDL_BlitSurface(texteSurface, NULL, SDL_render, &position);
        
        SDL_FreeSurface(texteSurface);
    }
    
    return SDL_render;
}

void Output::print(string line) {
    l_text.push_back(line.c_str());
    
    int textSize = TTF_RenderText_Blended(police, "test", couleur)->h;
    
    while(textSize*l_text.size() > h)
        l_text.erase(l_text.begin());
    
    parent->callUpdateRender();
}

/*
 * Accesseur
 */
string Output::getName() {
    return name;
}

int Output::getx() {
    return x;
}

int Output::gety() {
    return y;
}